<?php

class LogController extends GeanyController {

    // <editor-fold defaultstate="collapsed" desc="STATIC & PRIVATE VARIABLE DECLARATION">
    private $validator;
    const _FUNCTION_ID = 'log';
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="CONSTRUCTOR">
  
    public function __construct(){
        parent::__construct();
        $this->loadDefaultValue();
        $this->loadServices();
        $this->validator = null;
    }
	
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="ACTION-HANDLER">
  
    public function index(){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }
        
        $data['Model'] = $this->mService->getList();
        return View::make('log/index', $data);
    }

    public function create(){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }

        $log = new Log;
        
        try {
            if(Input::get('submit')==null){
                return $this->createInputView($log);
            }
            else{
                $log = $this->bindData();
                if($this->validateInput(Input::all())==FALSE) return $this->createInputView($log);
                $newId = $this->mService->insertObject($log);

                if($newId!=null){
                    Return Redirect::to('log/detail/'.$newId);
                }
                return $this->createInputView($log);
            }
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            return $this->createInputView($this->bindData());
        }

    }
    
    public function detail($id=null){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }
        
        try {
            $result = $this->mService->getObject($id);

            if($result==null) {
                PageData::addModelError("Log with Id [ ".$id." ] Not Found ");
                Return Redirect::to('log')->with('ModelState',PageData::getModelState());
            }
            $data['Model'] = $result;
            return View::make('log/detail', $data);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            Return Redirect::to('log');
        }
    }
    
    public function delete($id=null){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }

        try {
            $this->mService->deleteObject($id);
            
            Return Redirect::to('log');
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            Return Redirect::to('log');
        }
    }

    public function edit($id=null){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }
        
        $log = new Log;

        try {
            if(Input::get('submit')==null){
                $log = $this->mService->getObject($id);
                return $this->createInputView($log);
            }
            else{
                $log = $this->bindData();
                if($this->validateInput(Input::all())==FALSE) return $this->createInputView($log);
                
                if($this->mService->updateObject($log,$id)==true){
                    Return Redirect::to('log/detail/'.$log->getId());
                }
                return $this->createInputView($log);
            }
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            return $this->createInputView($this->bindData());
        }
        
    }

    
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="PRIVATE-FUNCTION">
    
    private function createInputView($log=null){
        $this->data['Model'] = $log;
        return View::make('log/input', $this->data);
    }
    
    private function bindData(){
        $data = Input::all();

        $log = New Log;
        $log->setName($data["name"]);
        $log->setRecordId($data["record_id"]);
        $log->setModuleId($data["module_id"]);
        $log->setStatusId($data["status_id"]);
        $log->setNote($data["note"]);
        
        return $log;
    }
 
    private function validateInput($data){
        
        if($data==null) return false;
        $rules = array(
            'name' => 'required'
        );

        $messages = array(
            'name' => 'Group Name is Required.'
        );
        
        
        $this->validator = Validator::make($data, $rules, $messages);
        if($this->validator->fails()) PageData::assignValidatorToModelState ($this->validator);
        return $this->validator->passes();
    }
    
    private function loadDefaultValue(){
        $this->setFunctionId(self::_FUNCTION_ID);
        $this->page_data['_MODULE_NAME'] = $this->getFunctionName();
    }

    private function loadServices(){
        $this->mService = new StatusService();
    }
    
    // </editor-fold>
    
    
}
?>